//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {}, //登录后的userInfo
    token: wx.getStorageSync('token'), // 登录后的token
    tel: null,
    extra: null,
    dashboardTodo: {
      electricity: 0,
      gas: 0,
      water: 0
    }, //抄表待办数量对象
    bookingTodo: 0, //预约看房待办数量
    rentTodo: 0,
    repairTodo: 0,
    authCheck: false, //权限-抄表信息
    authBook: false, //权限-预约信息
    authUser: false, //权限-租客信息
    authRepair: false, //权限-维修信息
  },
  onLoad: function () {
    // let self = this
    // wx.getSetting({
    //   success(res) {
    //     debugger
    //     self.setData({
    //       isAuthorized: res.authSetting['scope.userInfo'] || false
    //     })
    //   }
    // })
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      token: wx.getStorageSync('token'),
      tel: wx.getStorageSync('tel'),
      extra: wx.getStorageSync('extra')
    })
    
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh (e) {
    setTimeout(_ => {
    }, 1000)
  },
  /**
   * 上拉加载更多
   */
  onReachBottom(e) {
  },
  /**
   * 获取待办事项
   */
  getTodoList(url, targetName, data, navi) {
    app.globalData.request('get', url, { admin_id: wx.getStorageSync('id') }, navi).then(res => {
      this.setData({
        [targetName]: res.data
      })
    })
  },
  /** 根据类型跳转 */
  goToPage(e) {
    if (wx.getStorageSync('token')) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    } else {
      wx.showModal({
        title: '未登录',
        content: '您还未登录, 请登录',
        success(e) {
          if (e.confirm) {
            wx.navigateTo({
              url: '/pages/mine/login/login',
            })
          }
        }
      })
    }
  },
  /**
   * 重新授权
   */
  reAuthorize(e) {
    if(!this.data.userInfo.hasOwnProperty('nickName')) {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  },
  /**
   * 页面进入 就需要查询当前的用户信息 以保证是最新状态
   */
  onShow () {
    this.setData({
      'userInfo': wx.getStorageSync('userInfo') || {}, //登录后的userInfo
      'token': wx.getStorageSync('token') || null, // 登录后的token
      'tel': wx.getStorageSync('tel') || null, // 登录后的token
    })
    if (!wx.getStorageSync('token')){
      wx.showModal({
        title: '请登录',
        content: '员工您好, 请登录',
        showCancel: false,
        success(e) {
          if (e.confirm) {
            wx.navigateTo({
              url: '/pages/mine/login/login',
            })
          }
        },
      })
    } else {
      if (this.data.token) {
        this.getTodoList('applet/read_meter/todo', 'dashboardTodo')
        this.getTodoList('applet/order_house/todo', 'bookingTodo')
        this.getTodoList('/applet/repair/repairOrder', 'repairTodo')
      }
    }
    let auths = wx.getStorageSync('auth') || []
    auths.forEach(item => {
      switch (item) {
        case '118':
          this.setData({ authCheck : true })
          break
        case '119':
          this.setData({ authBook: true })
          break;
        case '120':
          this.setData({ authUser: true })
          break;
        case '121':
          this.setData({ authRepair: true })
          break;
        default:
          break;
      }
    })
  },
  /**
   * 点击登录
   */
  login() {
    // 未登录的情况则进入登录页 
    if (!this.data.token) {
      wx.navigateTo({
        url: '/pages/mine/login/login',
      })
    }
  }
})
